package com.ruoyi.project.product.specification.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.specification.domain.Specification;
import com.ruoyi.project.product.specification.service.ISpecificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品规格操作处理
 * @author Mai
 */
@Controller
@RequestMapping("product/specification")
public class SpecificationController extends BaseController {

    private String prefix="product/specification";

    @Autowired
    private ISpecificationService specificationService;

    /**
     * 跳转到查询页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("product:specification:view")
    public String specification(){
        return prefix + "/specification";
    }

    /**
     * 产品规格查询
     * @param specification
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("product:specification:list")
    public TableDataInfo list(Specification specification){
        startPage();
        List<Specification> list = specificationService.selectSpecificationList(specification);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(){
        return prefix + "/add";
    }

    /**
     * 产品规格新增
     * @param specification
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("product:specification:add")
    public AjaxResult addSave(Specification specification){
        //判断1是否等于返回的值，如果是等于1 ，就表中存在，不能添加数据。
        if(UserConstants.SERIES_NAME_NOT_UNIQUE.equals(specificationService.checkSpecificationNameUnique(specification))){
            return error("新增规格"+specification.getSpecificationName()+"失败，规格名称已存在");
        }
        return toAjax(specificationService.insertSpecification(specification));
    }

    /**
     * 规格修改
     * @param specificationId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{specificationId}")
    public String edit(@PathVariable("specificationId") Long specificationId, ModelMap mmap){
        mmap.put("specification",specificationService.selectSpecificationById(specificationId));
        return prefix + "/edit";
    }

    /**
     * 规格修改保存
     * @param specification
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions("product:specification:edit")
    public AjaxResult editSave(Specification specification){
        return toAjax(specificationService.updateSpecification(specification));
    }



    /**
     * 规格批量删除操作
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("product:specification:remove")
    public AjaxResult remove(String ids){
        try {
            return toAjax(specificationService.deleteSpecificationByIds(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    /**
     * 校验规格名称是否存在
     * @param specification
     * @return
     */
    @ResponseBody
    @PostMapping("/checkSpecificationNameUnique")
    public String checkSpecificationNameUnique(Specification specification){
        return specificationService.checkSpecificationNameUnique(specification);
    }

}
